package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcPatrolConfigMapper;
import com.csun.cmny.provider.dao.DmcPatrolGreatConfigMapper;
import com.csun.cmny.provider.model.domain.DmcPatrolConfig;
import com.csun.cmny.provider.model.domain.DmcPatrolGreatConfig;
import com.csun.cmny.provider.model.dto.patrol.DmcPatrolTaskVo;
import com.csun.cmny.provider.model.dto.patrol.PatroTaskPageDto;
import com.csun.cmny.provider.model.dto.patrol.PatrolTaskDto;
import com.csun.cmny.provider.model.vo.patrol.PatrolGreatConfigVo;
import com.csun.cmny.provider.service.DmcPatrolConfigService;
import com.csun.cmny.provider.service.DmcPatrolGreatConfigService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/7/22 9:40
 * @Description: 巡更棒配置接口实现
 */
@Service
public class DmcPatrolGreatConfigServiceImpl extends BaseService<DmcPatrolGreatConfig>
        implements DmcPatrolGreatConfigService {

    @Resource
    private DmcPatrolGreatConfigMapper dmcPatrolGreatConfigMapper;
    @Resource
    private DmcPatrolConfigMapper dmcPatrolConfigMapper;
    @Resource
    private DmcPatrolGreatConfigServiceImpl dmcPatrolGreatConfigService;
    @Resource
    private DmcPatrolConfigService dmcPatrolConfigService;


    /*
     * 新增巡更任务
     * */
    @Override
    public Integer patrolTaskAdd(DmcPatrolGreatConfig dmcPatrolGreatConfig) {
        return dmcPatrolGreatConfigMapper.insert(dmcPatrolGreatConfig);
    }
    /*
     * 查询巡更任务
     * */

    @Override
    public synchronized  PageInfo patrolSelectTask(PatrolTaskDto patrolTaskDto) {
        List<DmcPatrolTaskVo> DmcPatrolTaskVos = dmcPatrolGreatConfigMapper.patrolSelectTask(patrolTaskDto);
        if(DmcPatrolTaskVos.size()==0){
            PatroTaskPageDto patroTaskPageDto = new PatroTaskPageDto();
            patroTaskPageDto.setGroupId(patrolTaskDto.getGroupId());
            List<DmcPatrolGreatConfig>  dmcPatrolGreatConfigs = new ArrayList<>();
            List<DmcPatrolConfig> dmcPatrolConfigs = dmcPatrolConfigMapper.patrolTaskSelect(patroTaskPageDto);
            if(dmcPatrolConfigs.size()==0){
                return new PageInfo(dmcPatrolConfigs);
            }
            for (DmcPatrolConfig dmcPatrolConfig:dmcPatrolConfigs) {
                DmcPatrolGreatConfig dmcPatrolGreatConfig = new DmcPatrolGreatConfig();
                BeanUtils.copyProperties(dmcPatrolConfig,dmcPatrolGreatConfig);
                dmcPatrolGreatConfig.setDeviceId(patrolTaskDto.getDeviceId());
                dmcPatrolGreatConfigs.add(dmcPatrolGreatConfig);
            }
            Integer i = dmcPatrolGreatConfigMapper.savePatrolTask(dmcPatrolGreatConfigs);
            List<DmcPatrolTaskVo> dmcPatrolTaskVos = dmcPatrolGreatConfigMapper.patrolSelectTask(patrolTaskDto);
            return new PageInfo(dmcPatrolTaskVos);
        }
        return new PageInfo(DmcPatrolTaskVos);
    }

    /**
     * 查询巡更棒的配置
     *
     * @param greatId   the great id
     * @param groupId   the group id
     *
     * @return  the patrol great config vo list.
     */
    @Override
    public List<PatrolGreatConfigVo> queryConfigByGreatId(Long greatId, Long groupId) {

        List<PatrolGreatConfigVo> patrolGreatConfigVoList =
                dmcPatrolGreatConfigMapper.queryConfigByGreatId(greatId);

        if (PublicUtil.isEmpty(patrolGreatConfigVoList)) {
            patrolGreatConfigVoList = dmcPatrolConfigService.queryConfigByGroupId(groupId);
        }

        return patrolGreatConfigVoList;
    }
    /*
    * 删除巡更棒是删除设置的巡更时间
    * */
    @Override
    public void deleteByDeviceId(Long id) {
        dmcPatrolGreatConfigMapper.deleteByDeviceId(id);
    }
}
